package net.gq.logistics.shiro;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import net.gq.logistics.pojo.User;
import net.gq.logistics.pojo.UserExample;
import net.gq.logistics.service.PermissionService;
import net.gq.logistics.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/23
 */
public class CutomReaml extends AuthorizingRealm {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "permissionService")
    private PermissionService permissionService;


    /**
     * 授权方法
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        User user = (User) principals.getPrimaryPrincipal();


        //将当前认证的身份（用户）的角色权限id切割成数组
        String[] permissionIdsArr = user.getPermissionIds().split(",");

        List<Long> permissionIdsList = new ArrayList<Long>();

        for (String s : permissionIdsArr) {
            permissionIdsList.add(Long.valueOf(s));
        }

        //权限表达式
        List<String> permissions = permissionService.selectExpressionByIds(permissionIdsList);


        //创建授权信息对象
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        //将身份的角色设置Shiro
        authorizationInfo.addRole(user.getRolename());
        System.out.println("rolename ="+user.getRolename());

        //将权限表达式设置给shiro框架
        authorizationInfo.addStringPermissions(permissions);


        return authorizationInfo;
    }


    /**
     * 认证方法
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        //获取token（表单提交的账号）的身份
        String username = (String) token.getPrincipal();

        //从数据库中过呢据账号查询是否存在此用户
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userService.selectByExample(example);

        //如果数据库查无此账号，返回错误信息
        if (users.size() == 0) {
            return null;
        }

        //如果有，进行密码比对
        User user = users.get(0);

        ByteSource credentialsSalt = ByteSource.Util.bytes(user.getSalt());
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, this.getName());
        return authenticationInfo;

    }
}
